<?php
namespace app\admin\model;
use think\Config;
use think\Db;
class VideoModel extends \app\common\model\CommonModel
{
    public function getList($params)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;
            $field = "a.*,b.brand,b.team_video,b.team_operate,c.username,d.product_name";
            $sql = "SELECT {$field} FROM video a LEFT JOIN brand b ON a.brand_id = b.id 
                    LEFT JOIN user c ON a.upload_userid = c.userid 
                    LEFT JOIN product d ON a.product_id = d.id WHERE 1=1 ";
            // 前测状态
            if(!empty($params['status'])){
                $sql .= "AND a.status = '{$params['status']}' ";
            }
            // 标签
            if(!empty($params['tag'])){
                // $sql .= "AND a.tags LIKE '%{$params['tag']}%' ";
                $tag_arr = explode(',',$params['tag']);
                $or = '';
                foreach ($tag_arr as $key => $value) {
                    // if(isset($tag_arr[$key+1])){
                    //     $or .= "a.tags LIKE '%{$value}%' AND ";
                    // }else{
                    //     $or .= "a.tags LIKE '%{$value}%' ";
                    // }
                    if(isset($tag_arr[$key+1])){
                        $or .= "tags REGEXP '(,|^|、){$value}(,|$|、)' AND ";
                    }else{
                        $or .= "tags REGEXP '(,|^|、){$value}(,|$|、)' ";
                    }
                }
                $sql .= "AND ($or) ";
            }
            // 用户名
            if(!empty($params['username'])){
                $sql .= "AND c.username = '{$params['username']}' ";
            }
            // 编导userid
            if(!empty($params['direct_userid'])){
                $sql .= "AND a.direct_userid = '{$params['direct_userid']}' ";
            }
            // 摄像师userid
            if(!empty($params['camera_userid'])){
                $sql .= "AND a.camera_userid = '{$params['camera_userid']}' ";
            }
            // 品牌名
            if(!empty($params['brand'])){
                $sql .= "AND b.brand = '{$params['brand']}' ";
            }
            // 产品名
            if(!empty($params['product'])){
                $sql .= "AND d.product_name = '{$params['product']}' ";
            }
            // 视频名
            if(!empty($params['exact'])){
                $sql .= "AND a.name LIKE '%{$params['exact']}%' ";
            }
            // 是否推送过 1推送过 2未推送
            if(!empty($params['push']) && $params['push'] == 1){
                $sql .= "AND a.push_status = 'PUSHED' ";
            }
            if(!empty($params['push']) && $params['push'] == 2){
                $sql .= "AND a.push_status = 'WATING' ";
            }
            // 时间
            if(!empty($params['start']) && !empty($params['end'])){
                $sql .= "AND a.create_time BETWEEN '{$params['start']}' AND '{$params['end']}' ";
            }
            $order = "ORDER BY a.create_time DESC LIMIT {$start},{$pagesize}";
            $csql = str_replace($field,'count(*)',$sql);
            $sql .= $order;
            $res = $this->getRes($sql);
            if(!empty($res)){
                $res = array_map(function($v){
                    $v['push_count'] = Db::table('push_log')->where('relate_id',$v['id'])->count();
                    $v['direct_username'] = !empty($v['direct_userid']) ? Db::table('enum_user')->where('userid',$v['direct_userid'])->value('username') : '';
                    $v['camera_username'] = !empty($v['camera_userid']) ? Db::table('enum_user')->where('userid',$v['camera_userid'])->value('username') : '';
                    return $v;
                },$res);
            }
            $count = $this->getCount($csql);
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 获取推送日志
     * @param array  $params
     */
    public function getLog($params)
    {
        try {
            $page     = isset($params['page']) && is_numeric($params['page']) ? (int)$params['page'] : 1;
            $pagesize = isset($params['pagesize']) && is_numeric($params['pagesize']) ? (int)$params['pagesize'] : 10;
            $start = ($page-1) * $pagesize;
            $sql = "SELECT * FROM push_log WHERE relate_id = {$params['id']} ";
            $order = "ORDER BY create_time DESC LIMIT {$start},{$pagesize}";
            $csql = str_replace('*','count(*)',$sql);
            $sql .= $order;
            $res = $this->getRes($sql);
            $count = $this->getCount($csql);
            return $this->pageData($res,$pagesize,$page,$count);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}